# -*- coding: utf-8 -*-
"""
@Time ： 2023-05-29 22:21
@Author ： Jinbo CHEN
@File ：main.py
"""
from functools import reduce
from dematel import DEMATEL, Grey, DANP
from dwriter import write_dematel_matrices, write_grey_matrices, draw_causal_diagram, write_danp_matrix
import pandas as pd
if __name__ == '__main__':
    ######### level 1 factors G-DEMATEL
    # step1. collect expert assessment data
    # grey linguistic scale for influence scores
    gls = {
        0: [0, 0],
        1: [0, .25],
        2: [.25, .5],
        3: [.5, .75],
        4: [.75, 1]
    }
    # linguistic scale direct-relation matrix - expert 1-6
    assessments = []
    for num in [1, 2, 3, 4, 5, 6]:
        qe = f"questionnaires\\L1\\QT{num}.xlsx"
        assessment = pd.read_excel(qe, skiprows=11,
                                   nrows=5,
                                   usecols=[1, 2, 3, 4, 5, 6],
                                   index_col=0,
                                   names=['CODE', 'R1', 'R2', 'R3', 'R4', 'R5'])
        assessments.append(assessment)

    # step2. grey matrices defuzzification
    grey_list = [Grey(gls, i) for i in assessments]
    # overall crisp direct-relation matrix (Z)
    z_list = [g.Z for g in grey_list]
    overall_Z = reduce(lambda x, y: x + y, z_list) / len(z_list)

    # step3. DEMATEL calc.
    d1 = DEMATEL(overall_Z, [])

    # step4. Output results to excel
    excel_path = 'Level_1.xlsx'
    write_grey_matrices(excel_path, grey_list)
    write_dematel_matrices(excel_path, d1)
    # step5. draw causal diagram
    draw_causal_diagram(excel_path, "RDPE", "G2", d1.RDPE, d1.T)


    ######### level 2 factors G-DEMATEL
    # step1. collect expert assessment data
    # grey linguistic scale for influence scores
    gls = {
        0: [0, 0],
        1: [0, .25],
        2: [.25, .5],
        3: [.5, .75],
        4: [.75, 1]
    }
    # linguistic scale direct-relation matrix - expert 1-6
    assessments = []
    for num in [1, 2, 3, 4, 5, 6]:
        qe = f"questionnaires\\L2\\QE{num}.xlsx"
        assessment = pd.read_excel(qe, skiprows=11,
                                   nrows=19,
                                   usecols=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
                                   index_col=0,
                                   names=['CODE', 'R11', 'R12', 'R13', 'R14', 'R21', 'R22', 'R23',
                                          'R31', 'R32', 'R33', 'R34', 'R35', 'R36',
                                          'R41', 'R42', 'R43', 'R44', 'R51', 'R52'])
        assessments.append(assessment)

    # step2. grey matrices defuzzification
    grey_list = [Grey(gls, i) for i in assessments]
    # overall crisp direct-relation matrix (Z)
    z_list = [g.Z for g in grey_list]
    overall_Z = reduce(lambda x, y: x + y, z_list) / len(z_list)

    # step3. DEMATEL calc.
    split_sizes = [4, 3, 6, 4, 2]

    d2 = DEMATEL(overall_Z, split_sizes)

    # step4. Output matrices to excel
    excel_path = 'Level_2.xlsx'
    write_grey_matrices(excel_path, grey_list)
    write_dematel_matrices(excel_path, d2)
    # step5. output diagrams
    draw_causal_diagram(excel_path, "RDPE", "G2", d2.RDPEs[0], d2.T.iloc[:4, :4])
    draw_causal_diagram(excel_path, "RDPE", "G26", d2.RDPEs[1], d2.T.iloc[4:7, 4:7])
    draw_causal_diagram(excel_path, "RDPE", "G50", d2.RDPEs[2], d2.T.iloc[7:13, 7:13])
    draw_causal_diagram(excel_path, "RDPE", "G74", d2.RDPEs[3], d2.T.iloc[13:17, 13:17])
    draw_causal_diagram(excel_path, "RDPE", "G98", d2.RDPEs[4], d2.T.iloc[17:19, 17:19])
    draw_causal_diagram(excel_path, "RDPE", "G122", d2.RDPE, d2.T)


    ### DANP
    # define level 2 factors group size
    danp = DANP(d1.T, d2.T, split_sizes)
    write_danp_matrix(excel_path, danp)
    print(0)
    quit()
